package de.fhffm.scs.simulator.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import de.fhffm.scs.simulator.gui.composites.BodyVisualisation;
import de.fhffm.scs.simulator.gui.composites.RegulationVisulisation;
import de.fhffm.scs.simulator.gui.composites.SimulationController;
import de.fhffm.scs.simulator.gui.composites.UserInterface;

public class SimulationWindow {

	protected Shell shell;
	private BodyVisualisation bodyVisualisation;
	private RegulationVisulisation regulationVisulisation;
	private UserInterface userInterface;
	private SimulationController simulationController;

	private Simulation simulation;

	public SimulationWindow(Simulation s) {
		this.simulation = s;
	}

	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		System.exit(1);
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(900, 700);
		shell.setText("SWT Application");

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		shell.setLayout(gridLayout);

		GridData upperLeft = new GridData();
		upperLeft.horizontalAlignment = SWT.FILL;
		upperLeft.verticalAlignment = SWT.FILL;
		upperLeft.grabExcessVerticalSpace = true;

		GridData midLeft = new GridData();
		midLeft.horizontalAlignment = SWT.FILL;
		midLeft.verticalAlignment = SWT.FILL;
		midLeft.grabExcessVerticalSpace = true;

		GridData lowerLeft = new GridData();
		lowerLeft.horizontalAlignment = SWT.FILL;
		lowerLeft.verticalAlignment = SWT.FILL;
		lowerLeft.grabExcessVerticalSpace = true;

		GridData right = new GridData();
		right.verticalAlignment = SWT.FILL;
		right.verticalSpan = 3;
		right.horizontalSpan = 1;
		right.grabExcessVerticalSpace = true;
		right.horizontalAlignment = SWT.FILL;
		right.grabExcessHorizontalSpace = true;

		bodyVisualisation = new BodyVisualisation(simulation.getBody(), shell, SWT.None);
		bodyVisualisation.setLayoutData(upperLeft);

		regulationVisulisation = new RegulationVisulisation(simulation.getController(),
				shell, SWT.NONE);
		regulationVisulisation.setLayoutData(right);

		simulationController = new SimulationController(shell, SWT.NONE);
		simulationController.setLayoutData(midLeft);

		userInterface = new UserInterface(shell, SWT.NONE);
		userInterface.setLayoutData(lowerLeft);
		
		simulation.setIndicationDriver(userInterface);
		

		new Thread() {
			public void run() {
				while (true) {
					Display.getDefault().asyncExec(new Runnable() {
						public void run() {
							update();
						}
					});
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}

	public void update() {
		regulationVisulisation.update();
	}

}
